package org.mparts.client.activity;

import java.util.Date;
import java.util.List;

import org.mparts.client.ui.INewsView;
import org.mparts.client.ui.INewsView.INewsPresenter;
import org.mparts.client.ui.editor.INewsEditorView;
import org.mparts.client.ui.editor.INewsEditorView.INewsEditorPresenter;
import org.mparts.client.ui.events.LoadUserEvent;
import org.mparts.client.ui.events.LoadUserEventHandler;
import org.mparts.client.ui.events.ReloadPartitemListEvent;
import org.mparts.client.utils.Utils;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.NewsProxy;
import org.mparts.shared.request.MpartsRequest;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.sencha.gxt.data.shared.loader.ListLoadConfig;
import com.sencha.gxt.data.shared.loader.ListLoadResult;
import com.sencha.gxt.data.shared.loader.RequestFactoryProxy;

public class NewsActivity extends AbstractMpartsActivity implements INewsPresenter, INewsEditorPresenter {

	private final EventBus eventBus;
	private final INewsView display;
	private final INewsEditorView editor;

    @Inject
	public NewsActivity(MpartsRequestFactory rf, EventBus eventBus, INewsView display, INewsEditorView editor
			) {
    	this.rf = rf;
		this.eventBus = eventBus;
		this.display = display;
		this.editor = editor;
		
		bind();
	}
    
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		display.setPresenter(this);
	}
	
	private void bind() {
		display.setPresenter(this);
		editor.setPresenter(this);
		editor.initDriver(rf);
		display.getStore().clear();
		Log.debug("load news...");
//		rf.mpartsRequest().lastNews().fire(new Receiver<List<NewsProxy>>() {
//
//			@Override
//			public void onSuccess(List<NewsProxy> response) {
//				Log.debug("loaded news count: " + response.size());
//				display.loadStore(response);
//			}
//		});
		
		display.setProxy(
		new RequestFactoryProxy<ListLoadConfig, ListLoadResult<NewsProxy>>() {

			@Override
			public void load(ListLoadConfig loadConfig,
					Receiver<? super ListLoadResult<NewsProxy>> receiver) {
				MpartsRequest req = rf.mpartsRequest();
				req.lastNews().to(receiver);
				req.fire();
			}
			
		});
		
		eventBus.addHandler(LoadUserEvent.TYPE, new LoadUserEventHandler() {

			@Override
			public void onLoad(LoadUserEvent saveEvent) {
				display.showToolBar(Utils.isManager());
			}
		});
	}

	@Override
	public INewsView getView() {
		return display;
	}


	private void deleteNews(NewsProxy item) {
		MpartsRequest req = rf.mpartsRequest();
		req.deleteNews(req.edit(item));
		req.fire(new Receiver<Void>() {

			@Override
			public void onSuccess(
					Void response) {
				display.loadStore();
			}
		});
	}

	@Override
	public void actionNews(NewsProxy item, ACTIONS action) {
		MpartsRequest req = rf.mpartsRequest();
		NewsProxy currentEntity = item;
		switch (action) {
		case ADD:
			currentEntity = req.create(NewsProxy.class);
			currentEntity.setNewsDate(new Date());
			break;
		case EDIT:
			currentEntity = req.edit(currentEntity);
			break;
		case DELETE:
			deleteNews(currentEntity);
			return;
		}
		
		if ( currentEntity == null ) {
			Log.error("trying to edit null news item");
		} else {
			req.saveNews(currentEntity);
			editor.getDriver().edit(currentEntity, req);
			editor.getDialog().show();
		}
	}

	@Override
	public void saveNews() {
		RequestContext req = editor.getDriver().flush();
		if (!editor.getDriver().hasErrors()) {
			req.fire(new Receiver<Void>() {
				
				@Override
				public void onSuccess(Void response) {
					Log.debug("save news successful");
					display.loadStore();
					editor.getDialog().hide();
				}
			});
		}
	}

}
